package main

import (
	"fmt"

	"k8s.io/client-go/discovery"

	//"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/tools/clientcmd"
)

/*
*
@Author: cloudea
@Desc:  获取kube-system下的pod列表
*/
func main() {
	/** 1. 加载配置文件，生成配置对象 */
	config, err := clientcmd.BuildConfigFromFlags("", "/home/cloudea/.kube/config")
	if err != nil {
		panic(err.Error())
	}

	/** 实例化client set*/
	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)

	if err != nil {
		panic(err.Error())
	}

	/**
	请求过程：


	*/

	resourceList, err := discoveryClient.ServerPreferredResources()
	//discoveryClient.ServerPreferredResources()
	//discoveryClient.ServerGroups()
	//discoveryClient.ServerGroupsAndResources()
	//discoveryClient.ServerPreferredNamespacedResources()
	//discoveryClient.ServerResourcesForGroupVersion()
	//discoveryClient.ServerVersion()

	if err != nil {
		panic(err.Error())
	}

	for _, item := range resourceList {

		fmt.Printf("%s-%s-%s\n", item.GroupVersion, item.APIVersion, item.Kind)
		//fmt.Printf("%s\t%s\t%s\t%v\n", item.GroupVersion, item.APIVersion, item.Kind, item.APIResources)
		for _, res := range item.APIResources {
			fmt.Printf("%s\t%s\t%s\t%s\n", res.Group, res.Version, res.Kind, res.Name)
		}
	}
}
